package com.nuanshui.heatedloan.entity.cms.security;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.tuple.MutablePair;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.nuanshui.heatedloan.entity.IdEntity;
import com.nuanshui.heatedloan.extension.jpasearch.SearchType;
import com.nuanshui.heatedloan.extension.jpasearch.SearchTypeValue;

/**
 * 角色
 */
@Entity
@Table(name = "cms_security_role")
public class Role extends IdEntity {
	
	private static final long serialVersionUID = 1L;


	public Role()
	{
		needPermission=Boolean.FALSE;
	}

	/**
	 * 模块的名称
	 */
	@SearchType(value=SearchTypeValue.LIKE)
	@Column(nullable = false, length = 32)
	private String name;

	@JSONField(serialize=false)
	public Boolean getNeedPermission() {
		return needPermission;
	}

	public void setNeedPermission(Boolean needPermission) {
		this.needPermission = needPermission;
	}

	@Transient
	private Boolean needPermission;
	
	private Integer defaultRole;//是否是默认的角色， 如超级管理员，则不允许删除

	/**
	 * 操作模块的权限
	 */
	// 指定集合的嵌入式对象，这些集合是独立存储在集合表中
	@ElementCollection
	// 指定值存储在security_role_permission表中
	@CollectionTable(name = "cms_security_role_permission", joinColumns = { @JoinColumn(name = "roleId") })
	@Column(name = "permission")
	@JSONField(serialize=false)
	private List<String> permissionList = Lists.newArrayList();
	
	@Transient
	private Map<String, List<String>> permissionMap = new LinkedHashMap<String, List<String>>();

	/**
	 * Role表和UserRole关系是一对多
	 */
	@OneToMany(mappedBy = "role", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private List<UserRole> userRoles = new ArrayList<UserRole>(0);
	

	public Integer getDefaultRole() {
		return defaultRole;
	}

	public void setDefaultRole(Integer defaultRole) {
		this.defaultRole = defaultRole;
	}

	/**
	 * 返回 name 的值
	 * 
	 * @return name
	 */
	public String getName() {
		return name;
	}

	/**
	 * 设置 name 的值
	 * 
	 * @param name
	 */
	public void setName(final String name) {
		this.name = name;
	}

	/**
	 * 返回 userRoles 的值
	 * 
	 * @return userRoles
	 */
	@JsonIgnore
	@JSONField(serialize=false)
	public List<UserRole> getUserRoles() {
		return userRoles;
	}

	/**
	 * 设置 userRoles 的值
	 * 
	 * @param userRoles
	 */
	public void setUserRoles(final List<UserRole> userRoles) {
		this.userRoles = userRoles;
	}

	/**
	 * 返回 permissionList 的值
	 * 
	 * @return permissionList
	 */
	public List<String> getPermissionList() {
		return permissionList;
	}

	/**
	 * 设置 permissionList 的值
	 * 
	 * @param permissionList
	 */
	public void setPermissionList(final List<String> permissionList) {
		this.permissionList = permissionList;
	}



	public Map<String, List<String>> getPermissionMap() {
		if(needPermission==null || needPermission==false)
		{
			return null;
		}
		if(this.permissionMap != null && permissionMap.size() > 0) return  permissionMap;
		
		if(permissionList == null || permissionList.isEmpty()) return permissionMap;
		Map<String, Integer> map = new LinkedHashMap<String, Integer>();
		
		List<String> list ;
		int i = 1;
		int j = 0;
		for(String permission : permissionList){
			String[] str = permission.split(":");
			if(str.length != 2) continue;
			  
			if(permissionMap.get(str[0]) == null){
				list = Lists.newArrayList();
				permissionMap.put(str[0], list);
				
			}else{
				list = permissionMap.get(str[0]);
			}
			
			list.add(str[1]);
		} 
		System.out.println("本次循环的总数是"+ i +"次---------" + map +"map里面的总数是 " +j );
		return permissionMap;
	}

	public void setPermissionMap(Map<String, List<String>> permissionMap) {
		this.permissionMap = permissionMap;
	}
}
